package com.woniu.controller;

import com.woniu.entity.PageData;
import com.woniu.entity.Result;
import com.woniu.entity.vo.LoginLogVO;
import com.woniu.entity.vo.ResentLoginLogVO;
import com.woniu.pojo.LoginLog;
import com.woniu.service.AdminService;
import com.woniu.service.ILoginLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sss
 * @since 2022-12-26
 */
@RestController
@RequestMapping("/loginLog")
@Api(tags = "登录日志")
public class LoginLogController {
    @Autowired
    private ILoginLogService loginLogService;

    @Autowired
    private AdminService adminService;

    @ApiOperation("保存登录日志")
    @PostMapping
    public Result saveLoginLog(@RequestBody LoginLog loginLog) {
        boolean result = loginLogService.save(loginLog);
        if (result) {
            return new Result(200,"保存成功");
        }
        return new Result(500,"保存失败");
    }

    @ApiOperation("查找登录日志")
    @PostMapping("/list/{currentPage}/{pageSize}")
    public Result listLoginLog(@PathVariable Integer currentPage,
                               @PathVariable Integer pageSize,
                               HttpServletRequest request) throws UnsupportedEncodingException {
        String phone = null;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equals("token")) {
                    System.out.println("cookie:username=" + URLDecoder.decode(cookies[i].getValue(),"UTF-8"));
                    phone = cookies[i].getValue();
                }
            }
        } else {
            System.out.println("没有任何Cookie");
        }
        PageData listByPhone = loginLogService.listByPhone(currentPage, pageSize, phone);
        return new Result(200,"查找登录日志成功",listByPhone);
    }

    @ApiOperation("查找最近的登录信息")
    @GetMapping("/resent")
    public Result ResentLoginLog(HttpServletRequest request) {
        String phone = null;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equals("token")) {
                    phone = cookies[i].getValue();
                }
            }
        } else {
            System.out.println("没有任何Cookie");
        }
        ResentLoginLogVO resentLoginLogVO = loginLogService.resentLoginLogByPhone(phone);
        return new Result(200,"查询最近登录信息成功",resentLoginLogVO);
    }

    @ApiOperation("根据原密码修改密码")
    @PutMapping("/modify")
    public Result modifyPwd(@RequestParam String oldPass,
                            @RequestParam String newPass,
                            HttpServletRequest request){
        String phone = null;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equals("token")) {
                    phone = cookies[i].getValue();
                }
            }
        } else {
            System.out.println("没有任何Cookie");
        }
        Boolean flag = adminService.modifyPwd(oldPass,newPass,phone);
        if (flag) {
            return new Result(200, "密码修改成功");
        }
        return new Result(500,"密码修改失败，请核对后再试");
    }

}
